package com.sense.kukoo.biz_me.view

import android.os.Bundle
import android.view.View
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.sense.kukoo.biz_me.R
import com.sense.kukoo.biz_me.databinding.BizMeActivityPhoneNumberBinding
import com.sense.kukoo.biz_me.vm.PhoneNumberViewModel
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.event.CountrySelectEvent
import com.sense.kukoo.common.event.LoginSuccessEvent
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.bean.AreaBean
import com.sense.kukoo.common.utils.KeyBoardUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = ARouterUrl.ROUTE_ME_MODULE_PHONE_NUMBER_ACTIVITY)
class PhoneNumberActivity : BaseMvvmActivity<PhoneNumberViewModel, BizMeActivityPhoneNumberBinding>() {

    private val TAG: String = javaClass.simpleName

    override fun getLayoutId(): Int = R.layout.biz_me_activity_phone_number

    override fun getViewModelClass(): Class<PhoneNumberViewModel> = PhoneNumberViewModel::class.java

    override fun responseVM(action: Int) {}

    override fun initData() {
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        mBinding.titleView.init(this, "", false)
        setSupportActionBar(mBinding.titleView)
        //
        bindAction(mViewModel.action_toVerifyCode){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_ME_MODULE_VERIFY_CODE_ACTIVITY)
                .withString(ARouterUrl.KEY_AREA_CODE, mViewModel.mAreaCode.value)
                .withString(ARouterUrl.KEY_PHONE_NUMBER, mViewModel.mPhoneNumber.value)
                .navigation()
        }
        bindAction(mViewModel.action_toSelectCountry){
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_ME_MODULE_SELECT_COUNTRY_ACTIVITY)
                .navigation()
        }
        //
        bindData(mViewModel.mIsShowPhoneErrorHit){
            mBinding.tvPhoneErrorHint.visibility = if(it) View.VISIBLE else View.GONE
        }
        bindData(mViewModel.mBtnEnable){
            if(it){
                mBinding.ivBtnEnter.setImageResource(R.drawable.biz_me_icon_enter_phone_enabled)
            }else{
                mBinding.ivBtnEnter.setImageResource(R.drawable.biz_me_icon_enter_phone_disable)
            }
        }
    }

    override fun initView() {
        mBinding.etPhoneNumber.addTextChangedListener(afterTextChanged = {
            mViewModel.onEditChanged()
        })
        //
        mBinding.etPhoneNumber.requestFocus()
        KeyBoardUtils.openKeybord(mBinding.etPhoneNumber, this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: LoginSuccessEvent) {
        finish()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: CountrySelectEvent) {
        val data: AreaBean = event.data
        mBinding.tvAreaCode.text = data.code
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        mBinding.etPhoneNumber.clearFocus()
        KeyBoardUtils.closeKeybord(mBinding.etPhoneNumber, this)
        //
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}